﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.CUiLinearJogModeGVCursorMgr
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace DnProcessSimulateCommands.RobotJog;

internal class CUiLinearJogModeGVCursorMgr
{
  private Cursor _defaultCursor;
  private Dictionary<string, Cursor> _storedCursors = new Dictionary<string, Cursor>();

  public void Initialize()
  {
    if (this._defaultCursor != (Cursor) null)
    {
      this._defaultCursor.Dispose();
      this._defaultCursor = (Cursor) null;
    }
    this._defaultCursor = TxGraphicViewerEx.The.Cursor;
  }

  public void Uninitialize()
  {
    if (this._defaultCursor != (Cursor) null)
    {
      this._defaultCursor.Dispose();
      this._defaultCursor = (Cursor) null;
    }
    this.DisposeStoredCursors();
  }

  public void SetCursor(CUiLinearJogModeGVCursorMgr.eCursorType cursorType)
  {
    Cursor cursor1 = (Cursor) null;
    switch (cursorType)
    {
      case CUiLinearJogModeGVCursorMgr.eCursorType.Default:
        cursor1 = this._defaultCursor;
        break;
      case CUiLinearJogModeGVCursorMgr.eCursorType.Active:
        cursor1 = this.LoadColorCursor("LinearJogModeActive.cur");
        break;
      case CUiLinearJogModeGVCursorMgr.eCursorType.ValidPick:
        cursor1 = this.LoadColorCursor("LinearJogModeValidPick.cur");
        break;
      case CUiLinearJogModeGVCursorMgr.eCursorType.InvalidPick:
        cursor1 = this.LoadColorCursor("LinearJogModeInvalidPick.cur");
        break;
    }
    Cursor cursor2 = (Cursor) null;
    if (cursor1 != (Cursor) null)
      cursor2 = new Cursor(cursor1.CopyHandle());
    try
    {
      TxGraphicViewerEx.The.Cursor = cursor2;
    }
    finally
    {
      if (cursor2 != (Cursor) null)
        cursor2.Dispose();
    }
  }

  private Cursor LoadColorCursor(string resourceName)
  {
    string str = "RobotJog.Resources." + resourceName;
    Cursor cursor = this.GetStoredCursor(str);
    if (cursor == (Cursor) null)
    {
      cursor = this.LoadCursorFromEmbeddedResource(str);
      if (cursor != (Cursor) null)
        this.StoreCursor(str, cursor);
    }
    return cursor;
  }

  private Cursor LoadMonochromeCursor(string resourceName)
  {
    string str = "Resources." + resourceName;
    Cursor cursor = this.GetStoredCursor(str);
    if (cursor == (Cursor) null)
    {
      cursor = new Cursor(this.GetType(), str);
      this.StoreCursor(str, cursor);
    }
    return cursor;
  }

  [DllImport("User32.dll", CharSet = CharSet.Ansi, ThrowOnUnmappableChar = true, BestFitMapping = false)]
  private static extern IntPtr LoadCursorFromFile(string str);

  private Cursor LoadCursorFromEmbeddedResource(string resourceRelativePath)
  {
    Cursor cursor = (Cursor) null;
    try
    {
      Assembly assembly = this.GetType().Assembly;
      string str = $"{assembly.GetName().Name}.{resourceRelativePath}";
      using (Stream manifestResourceStream = assembly.GetManifestResourceStream(str))
      {
        string tempCursorFile = this.CreateTempCursorFile("DnPSC", this.ExtractCursorFileExtensionFromResourceName(str), manifestResourceStream);
        cursor = new Cursor(CUiLinearJogModeGVCursorMgr.LoadCursorFromFile(tempCursorFile));
        File.Delete(tempCursorFile);
      }
    }
    catch
    {
    }
    return cursor;
  }

  private string ExtractCursorFileExtensionFromResourceName(string resourceName)
  {
    string fromResourceName = string.Empty;
    int num = resourceName.LastIndexOf('.');
    if (num > 0)
      fromResourceName = resourceName.Substring(num + 1);
    if (fromResourceName != "cur" && fromResourceName != "ani")
      fromResourceName = "cur";
    return fromResourceName;
  }

  private string CreateTempCursorFile(string fileNamePrefix, string fileExtension, Stream stream)
  {
    string uniqueFileName = this.GetUniqueFileName(Path.GetTempPath(), fileNamePrefix, fileExtension);
    using (FileStream destination = File.Create(uniqueFileName))
    {
      stream.Seek(0L, SeekOrigin.Begin);
      stream.CopyTo((Stream) destination);
    }
    return uniqueFileName;
  }

  private string GetUniqueFileName(string dirPath, string fileNamePrefix, string fileExtension)
  {
    string path2_1 = $"{fileNamePrefix}_{Guid.NewGuid()}.{fileExtension}";
    string path;
    string path2_2;
    for (path = Path.Combine(dirPath, path2_1); File.Exists(path); path = Path.Combine(dirPath, path2_2))
      path2_2 = $"{fileNamePrefix}_{Guid.NewGuid()}.{fileExtension}";
    return path;
  }

  private Cursor GetStoredCursor(string id)
  {
    Cursor cursor;
    return this._storedCursors.TryGetValue(id, out cursor) ? cursor : (Cursor) null;
  }

  private void StoreCursor(string id, Cursor cursor) => this._storedCursors.Add(id, cursor);

  private void DisposeStoredCursors()
  {
    foreach (Cursor cursor in this._storedCursors.Values)
    {
      if (cursor != (Cursor) null)
        cursor.Dispose();
    }
    this._storedCursors.Clear();
  }

  public enum eCursorType
  {
    Default,
    Active,
    ValidPick,
    InvalidPick,
  }
}
